package com.geek.thread.thread_local;

/**
 * 线程副本测试
 */
public class ThreadLocalTest {

	volatile Integer integer = 0;

	ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

	class TestThread extends Thread{

		int value;
		public TestThread(int value){
			this.value = value;
		}

		@Override
		public void run() {
			int val = 0;
//			synchronized (TestThread.class){
				for (int i = 0; i < 100; i++) {
//				    Integer integer = threadLocal.get();
//					if(integer == null){
//						integer = 0;
//					}
					integer++;
					threadLocal.set(integer);
				}
				System.out.println(integer + "," + threadLocal.get());
//			}
		}
	}

	void start() throws Exception{
		for (int i = 0; i < 3; i++) {
			new TestThread(i).start();
		}
		Thread.sleep(1000);
		System.out.println(integer + "," + threadLocal.get());
	}

	public static void main(String[] args) throws Exception {
		ThreadLocalTest test = new ThreadLocalTest();
		test.start();
	}

}
